package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车业务层
 *
 * @author wkh
 * @date 2024/6/17 16:53
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
       // 查询购物车
        ShoppingCart shoppingCart1 = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart1);
        shoppingCart1.setUserId(BaseContext.getCurrentId());
       List<ShoppingCart> shoppingCartList = shoppingCartMapper.shoppingCartList(shoppingCart1);

       // 判断是否存在商品
       if (shoppingCartList != null && !shoppingCartList.isEmpty()) {
           // 如果存在则数量加一
           ShoppingCart shoppingCart = shoppingCartList.get(0);
           shoppingCartMapper.updateNumber(shoppingCart.getNumber() + 1, shoppingCart.getId());
       } else {
           // 如果不存在则添加
           // 判断添加商品是菜品还是套餐
           ShoppingCart shoppingCart = null;
           if (shoppingCartDTO.getDishId() != null) {
               // 封装购物车对象
               Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
               shoppingCart = ShoppingCart.builder()
                       .dishId(dish.getId())
                       .dishFlavor(shoppingCartDTO.getDishFlavor())
                       .name(dish.getName())
                       .image(dish.getImage())
                       .amount(dish.getPrice())
                       .build();
           } else {
               Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
               shoppingCart = ShoppingCart.builder()
                       .setmealId(setmeal.getId())
                       .name(setmeal.getName())
                       .image(setmeal.getImage())
                       .amount(setmeal.getPrice())
                       .build();
           }
           shoppingCart.setUserId(BaseContext.getCurrentId());
           shoppingCart.setNumber(1);
           shoppingCart.setCreateTime(LocalDateTime.now());
           shoppingCartMapper.insert(shoppingCart);
       }
    }

    // 展示购物车
    @Override
    public List<ShoppingCart> showShoppingCart() {
        List<ShoppingCart> list = shoppingCartMapper.getByUserId(BaseContext.getCurrentId());
        return list;
    }

    // 清空购物车
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    // 删除购物车
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 查询购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.shoppingCartList(shoppingCart);
        shoppingCart = shoppingCartList.get(0);
        // 判断数量是否为1
        if (shoppingCart.getNumber() == 1) {
            // 如果是则删除
            shoppingCartMapper.deleteById(shoppingCart.getId());
        } else {
            // 如果不是则数量减一
            shoppingCartMapper.updateNumber(shoppingCart.getNumber() - 1, shoppingCart.getId());
        }
    }
}
